package com.mushuilingfeng.ddd.user.infrastructure.base;

import com.mushuilingfeng.ddd.user.domain.base.service.StorageAtomicIntegerService;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 示例demo，仅做演示
 *
 * @author
 * @date 2024/9/23
 */
@Service
public class StorageAtomicIntegerServiceImpl implements StorageAtomicIntegerService {
    private final Map<String, AtomicInteger> data = new ConcurrentHashMap<>();

    @Override
    public int getAndIncrement(String key) {
        AtomicInteger atomicInteger = data.computeIfAbsent(key, k -> new AtomicInteger(1));
        return atomicInteger.getAndIncrement();
    }

}
